package com.lianniu.mgr.baseService.dict.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.baseService.dict.dao.IDictTypeDao;
import com.lianniu.mgr.baseService.dict.model.DictTypeVo;
import com.lianniu.mgr.baseService.dict.service.IDictTypeService;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.util.StringUtil;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;


@Service
public class DictTypeServiceImpl implements IDictTypeService {

  private final static Logger logger = Logger.getLogger(DictTypeServiceImpl.class);
  
  @Resource
  IDictTypeDao dictTypeDao;
  
  
  @Override
  public BaseResult insertDictType(DictTypeVo dictType) {
    // TODO Auto-generated method stub
    
    logger.info("insertDictType begin");
    logger.info("insertDictType: input data check begin");
    logger.info("insertDictType dictType:" +JSON.toJSONString(dictType));
   
    
    BaseResult baseResult;
    if (StringUtil.isEmpty(dictType.getId())) {
      baseResult =
          BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    if (StringUtil.isEmpty(dictType.getTypeName())) {
      baseResult =
          BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    
    logger.info("insertDictType: input data check end");
    
    return BaseResult.success(dictTypeDao.insert(dictType));
  }

  @Override
  public BaseResult selectDictTypebyName(DictTypeVo dictType,PaginationInfo paginationInfo) {
    // TODO Auto-generated method stub    
    logger.info("selectDictTypebyName begin.");
    if (!StringUtil.isEmpty(dictType.getTypeName())) {
      dictType.setTypeName(dictType.getTypeName().replace(" ", ""));
    }
    logger.info("selectDictTypebyName typeName:" +dictType.getTypeName());
    logger.info("selectDictTypebyName paginationInfo:" +JSON.toJSONString(paginationInfo));
    
    dictType.setPaginationInfo(paginationInfo);
    List<DictTypeVo> list=dictTypeDao.selectByTypeNameForPage(dictType);
    Map<String,Object> retmap=new HashMap<String,Object>();
    retmap.put("total", paginationInfo.getTotalRecord());
    retmap.put("curPage", paginationInfo.getCurPage());
    retmap.put("rows", list);

    return BaseResult.success(retmap);     
  
  }

  @Override
  public BaseResult updateDictTypeById(DictTypeVo curDictType) {
    // TODO Auto-generated method stub
    logger.info("updateDictTypeById begin");
    logger.info("updateDictTypeById: update data check begin");
    logger.info("updateDictTypeById curDictType:" +JSON.toJSONString(curDictType));
  
    
    BaseResult baseResult;
    if (StringUtil.isEmpty(curDictType.getId())) {
      baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    if (StringUtil.isEmpty(curDictType.getTypeName())) {
      baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }    
    
    logger.info("updateDictTypeById: update data check end");
    
    return BaseResult.success(dictTypeDao.updateById(curDictType));
  }

  @Override
  public BaseResult delDictTypeById(String Id) {
    // TODO Auto-generated method stub    
    logger.info("delDictTypeById begin");
    logger.info("delDictTypeById Id:" +Id);
  
    return BaseResult.success(dictTypeDao.deleteById(Id));
  }

  /* (non Javadoc) 
   * @Title: selectDictTypeById
   * @Description: TODO

   * @param dictType
   * @return 
   * @see com.lianniu.mgr.baseService.dict.service.IDictTypeService#selectDictTypeById(com.lianniu.mgr.baseService.dict.model.DictTypeVo)
   * @date: 2016年4月2日 下午9:16:39 
   */
  @Override
  public BaseResult selectDictTypeById(DictTypeVo dictType) {
    // TODO Auto-generated method stub
    logger.info("selectDictTypeById begin");
    logger.info("selectDictTypeById Id:" +dictType.getId());
  
    return BaseResult.success(dictTypeDao.selectById(dictType.getId()));
  }

  /* (non Javadoc) 
   * @Title: deleteDictTypeBatch
   * @Description: TODO

   * @param id
   * @return 
   * @see com.lianniu.mgr.baseService.dict.service.IDictTypeService#deleteDictTypeBatch(java.lang.String[])
   * @date: 2016年4月2日 下午10:02:01 
   */
  @Override
  public BaseResult deleteDictTypeBatch(String... id) {
    // TODO Auto-generated method stub
    logger.info("deleteDictTypeBatch begin");
    logger.info("deleteDictTypeBatch Ids:" +id.toString());
    logger.info("deleteDictTypeBatch: input data check begin");
   
    BaseResult baseResult;
    if (StringUtil.isEmpty(id.toString())) {
      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    logger.info("deleteDictTypeBatch: input data check end");
    
    return BaseResult.success(dictTypeDao.deleteDictTypeBatch(id));
  }

  /* (non Javadoc) 
   * @Title: checkTypeName
   * @Description: TODO

   * @param typeName
   * @return 
   * @see com.lianniu.mgr.baseService.dict.service.IDictTypeService#checkTypeName(java.lang.String)
   * @date: 2016年4月5日 下午5:06:53 
   */
  @Override
  public Boolean checkTypeName(String typeName) {
 // TODO Auto-generated method stub
    logger.info("checkTypeName begin");
    logger.info("checkTypeName typeName:" +typeName.toString());
    logger.info("checkTypeName: input data check begin");   
  
    if (StringUtil.isEmpty(typeName)) {       
      return false;
    }
    logger.info("checkTypeName: input data check end");
    
    DictTypeVo dictType=new DictTypeVo();
    dictType.setTypeName(typeName);
    int count=dictTypeDao.count(dictType);
    if(count>0){
      return false;      
    }
    else{
      return true;
    }    
  }

  /* (non Javadoc) 
   * @Title: delBatchDictTypeLogic
   * @Description: TODO

   * @param id
   * @param updateUser
   * @return 
   * @see com.lianniu.mgr.baseService.dict.service.IDictTypeService#delBatchDictTypeLogic(java.lang.String[], java.lang.String)
   * @date: 2016年4月8日 上午9:02:40 
   */
  @Override
  public BaseResult delBatchDictTypeLogic(String updateUser,String...id) {
    // TODO Auto-generated method stub
    logger.info("delBatchDictTypeLogic begin");
    logger.info("delBatchDictTypeLogic Ids:" +id.toString());
    logger.info("delBatchDictTypeLogic: input data check begin");
   
    BaseResult baseResult;
    if (StringUtil.isEmpty(id.toString())) {
      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    logger.info("delBatchDictTypeLogic: input data check end");
    
    return BaseResult.success(dictTypeDao.delBatchDictTypeLogic(updateUser, id));
  }

}
